 ********************************************
 * Table 4: CALCULATE ELASTICITIES OF PRICE 
 * The elasticities use the model  computed in Table3_estimate_RPM_and_CL
 ********************************************
tab typeprov

* for each type of provider increase the price (for everybody) by 1%
forvalues i=1/4 {

preserve
set seed  563531

*list typeprov female p_i1 p_i2 p_i3 p_i4  p_i1_fem p_i2_fem p_i3_fem p_i4_fem in 1/3 //*to check changes are correct//*
*list typeprov female p_i1 p_i2 p_i3 p_i4  p_i1_fem p_i2_fem p_i3_fem p_i4_fem in 24/26 //*to check changes are correct//*

replace p_i1 = p_i1*(1.01) if typeprov==`i' 
replace p_i2 = p_i2*(1.01) if typeprov==`i'
replace p_i3 = p_i3*(1.01) if typeprov==`i' 
replace p_i4 = p_i4*(1.01) if typeprov==`i'

*list typeprov p_i1 p_i2 p_i3 p_i4  in 1/3  //*to check changes are correct//*

mixlpred PR1`i', nrep(500)

gen el_p`i' = 100*(PR1`i'-PR0)/PR0

display "THESE ARE OWN AND CROSS PRICE ELASTICiTIES FOR 1% INCREASE IN PRICE IN TYPEPROVIDER= `i' "
*bysort quartile typeprov: sum el_p`i'
table typeprov, c(mean el_p`i' med el_p`i') format(%9.3f) 

*table typeprov female, c(mean el_p`i' med el_p`i') format(%9.3f) col row

*table typeprov quartile, c(mean el_p`i' med el_p`i') format(%9.3f) col row

restore 
}


********************************************************************************************
 * BOOTSTRAPPING PRICE ELASTICITY IN MAIN MODEL 
********************************************************************************************
** This do file bootstraps price elasticities of demand  and price elasticities of demand by income quantile and by gender
** and save the simulated elasticities in file mixboot_g.dta

set seed 545415
local bsize=100 /* size of the iterations*/
global regressors "dist_dw_hf_2 dist_22   nu_8d  alt2-alt4 nu_8dsq"
global random "doctor_tot  generator2009 beds p_i1 p_i2 p_i3 p_i4  "
global control "idid choicesit quartile no_facility i1 i2 i3 i4 p_i1 p_i2 p_i3 p_i4 male female age age2 typeprov hh_id09 q01_10" 
set more off
tempname 3
postfile `3' iter   el_p11 el_p12 el_p13 el_p14  /// *self*
					el_p21 el_p22 el_p23 el_p24  /// *public*
					el_p31 el_p32 el_p33 el_p34  /// *private*
					el_p41 el_p42 el_p43 el_p44  /// *doctor*
					///
					el_pc111 el_pc121 el_pc131 el_pc141  /// *changing price self: quartile 1*
					el_pc112 el_pc122 el_pc132 el_pc142  /// *changing price self: quartile 2*
					el_pc113 el_pc123 el_pc133 el_pc143  /// *changing price self: quartile 3*
					el_pc114 el_pc124 el_pc134 el_pc144  /// *changing price self: quartile 4*
					///
					el_pc211 el_pc221 el_pc231 el_pc241  /// *changing price public: quartile 1*
					el_pc212 el_pc222 el_pc232 el_pc242  /// *changing price public: quartile 2*
					el_pc213 el_pc223 el_pc233 el_pc243  /// *changing price public: quartile 3*
					el_pc214 el_pc224 el_pc234 el_pc244  /// *changing price public: quartile 4*
					///
					el_pc311 el_pc321 el_pc331 el_pc341  /// *changing price private: quartile 1*
					el_pc312 el_pc322 el_pc332 el_pc342  /// *changing price private: quartile 2*
					el_pc313 el_pc323 el_pc333 el_pc343  /// *changing price private: quartile 3*
					el_pc314 el_pc324 el_pc334 el_pc344  /// *changing price private: quartile 4*
					///
					el_pc411 el_pc421 el_pc431 el_pc441  /// *changing price doctor: quartile 1*
					el_pc412 el_pc422 el_pc432 el_pc442  /// *changing price doctor: quartile 2*
					el_pc413 el_pc423 el_pc433 el_pc443  /// *changing price doctor: quartile 3*
					el_pc414 el_pc424 el_pc434 el_pc444  /// *changing price doctor: quartile 4*
                    using mixboot_nu8d_price, replace   
					
forvalues iters1=1(1)`bsize' {
use tempdata_mixedlogit_vs4.dta, clear
*	replace waittime2009 = -waittime2009
*	replace nomed2009 = -nomed2009
keep idid no_facility d_ $control $regressors $random
bsample 1000, cluster(idid) idcluster(idnew)
replace choicesit =idnew


				noisily di "  ITER =" "`iters1'" "ESTIMATION OF RANDOM PARAMETER MODEL "
				* estimate RPM for bootstrapped sample 'iter'
			    mixlogit d_  $regressors  , group(choicesit) id(idnew) cluster(q01_10) rand($random) from(b53, copy)

	            mixlpred PR0, nrep(500)
	            
* CALCULATE  THE SIMULATED ELASTICITY BY CHANGING X BY 1%
* for each type of provider increase the price (for everybody) by 1%
forvalues i=1/4 {
noisily di "WE ARE GOING TO CHANGE PRICE FOR PROVIDER=" "`i'" 

local i=`i'     //*typeprovider whose price is being changed*//
preserve

*set seed 563531
replace p_i1 = p_i1*(1.01) if typeprov==`i' 
replace p_i2 = p_i2*(1.01) if typeprov==`i'
replace p_i3 = p_i3*(1.01) if typeprov==`i' 
replace p_i4 = p_i4*(1.01) if typeprov==`i'

*list typeprov p_i1 p_i2 p_i3 p_i4  in 1/3  //*to check changes are correct//*

mixlpred PR1`i', nrep(500)

gen el_p`i' = 100*(PR1`i'-PR0)/PR0


* own and cross elasticities: for each provider (whose price changes), store own elasticity and cross
* if price changes in i, what happen to # => E#z(i)

noisily di "===> OWN AND CROSS ELASTICITY: PRICE CHANGE IN PROVIDER=" "`i'" 

sum el_p`i' if typeprov==1
local el_p`i'1 = r(mean)
sum el_p`i' if typeprov==2
local el_p`i'2 = r(mean)
sum el_p`i' if typeprov==3
local el_p`i'3 = r(mean)
sum el_p`i' if typeprov==4
local el_p`i'4 = r(mean)

/*
noisily di "===> OWN AND CROSS ELASTICITY FOR MALE: PRICE CHANGE IN PROVIDER=" "`i'" 

sum el_p`i' if typeprov==1 & female==0
local el_p`i'1m = r(mean)
sum el_p`i' if typeprov==2 & female==0
local el_p`i'2m = r(mean)
sum el_p`i' if typeprov==3 & female==0
local el_p`i'3m = r(mean)
sum el_p`i' if typeprov==4 & female==0
local el_p`i'4m = r(mean)

noisily di "===> OWN AND CROSS ELASTICITY FOR FEMALE: PRICE CHANGE IN PROVIDER=" "`i'" 

sum el_p`i' if typeprov==1 & female==1
local el_p`i'1f = r(mean)
sum el_p`i' if typeprov==2 & female==1
local el_p`i'2f = r(mean)
sum el_p`i' if typeprov==3 & female==1
local el_p`i'3f = r(mean)
sum el_p`i' if typeprov==4 & female==1
local el_p`i'4f = r(mean)

* generate the difference between elasticity of female and male 
noisily di "===> GET THE DIFFERENCE IN PRICE ELASTICITIES FEMALE-MALE FOR A 1% PRICE CHANGE IN PROVIDER=" "`i'" 

ttest el_p`i' if typeprov==1, by(female)
local del_p`i'1 = r(mu_2)-r(mu_1)
ttest el_p`i' if typeprov==2, by(female)
local del_p`i'2 = r(mu_2)-r(mu_1)
ttest el_p`i' if typeprov==3, by(female)
local del_p`i'3 = r(mu_2)-r(mu_1)
ttest el_p`i' if typeprov==4, by(female)
local del_p`i'4 = r(mu_2)-r(mu_1)

* for each income quartile sum elasticity  within group x and store elasticity
* interpretation: price elasticity - changing price of provider i, what happen to provider # and quartile q => E#z(i)

*forvalues q=1/2 {
*sum el_p`i' if typeprov==1 & quartile==`q'
*local el_pc`i'1`q' = r(mean)
*sum el_p`i' if typeprov==2 & quartile==`q'
*local el_pc`i'2`q' = r(mean)
*sum el_p`i' if typeprov==3 & quartile==`q'
*local el_pc`i'3`q' = r(mean)
*sum el_p`i' if typeprov==4 & quartile==`q'
*local el_pc`i'4`q' = r(mean)

*/
foreach  q of numlist 1 2 3 4 {

noisily di "===> OWN AND CROSS ELASTICITY BY INCOME QUANTILE: PRICE CHANGE IN PROVIDER=" "`i'" "  INCOME QUARTILE=" "`q'"

sum el_p`i' if typeprov==1 & quartile==`q'
local el_pc`i'1`q' =r(mean)
sum el_p`i' if typeprov==2 & quartile==`q'
local el_pc`i'2`q' =r(mean)
sum el_p`i' if typeprov==3 & quartile==`q'
local el_pc`i'3`q' =r(mean)
sum el_p`i' if typeprov==4 & quartile==`q'
local el_pc`i'4`q' =r(mean)
}


restore


}

local iter=`iters1'


post `3' (`iter')   (`el_p11') (`el_p12') (`el_p13') (`el_p14')  /// *self*
					(`el_p21') (`el_p22') (`el_p23') (`el_p24')  /// *public*
					(`el_p31') (`el_p32') (`el_p33') (`el_p34')  /// *private*
					(`el_p41') (`el_p42') (`el_p43') (`el_p44')  /// *doctor*
					///
					(`el_pc111') (`el_pc121') (`el_pc131') (`el_pc141')  /// *changing price self: quartile 1*
					(`el_pc112') (`el_pc122') (`el_pc132') (`el_pc142')  /// *changing price self: quartile 2*
					(`el_pc113') (`el_pc123') (`el_pc133') (`el_pc143')  /// *changing price self: quartile 3*
					(`el_pc114') (`el_pc124') (`el_pc134') (`el_pc144')  /// *changing price self: quartile 4*
					///
					(`el_pc211') (`el_pc221') (`el_pc231') (`el_pc241')  /// *changing price public: quartile 1*
					(`el_pc212') (`el_pc222') (`el_pc232') (`el_pc242')  /// *changing price public: quartile 2*
					(`el_pc213') (`el_pc223') (`el_pc233') (`el_pc243')  /// *changing price public: quartile 3*
					(`el_pc214') (`el_pc224') (`el_pc234') (`el_pc244')  /// *changing price public: quartile 4*
					///
					(`el_pc311') (`el_pc321') (`el_pc331') (`el_pc341')  /// *changing price private: quartile 1*
					(`el_pc312') (`el_pc322') (`el_pc332') (`el_pc342')  /// *changing price private: quartile 2*
					(`el_pc313') (`el_pc323') (`el_pc333') (`el_pc343')  /// *changing price private: quartile 3*
					(`el_pc314') (`el_pc324') (`el_pc334') (`el_pc344')  /// *changing price private: quartile 4*
					///
					(`el_pc411') (`el_pc421') (`el_pc431') (`el_pc441')  /// *changing price doctor: quartile 1*
					(`el_pc412') (`el_pc422') (`el_pc432') (`el_pc442')  /// *changing price doctor: quartile 2*
					(`el_pc413') (`el_pc423') (`el_pc433') (`el_pc443')  /// *changing price doctor: quartile 3*
					(`el_pc414') (`el_pc424') (`el_pc434') (`el_pc444')  /// *changing price doctor: quartile 4*
					
					
}
postclose `3'
		

		
use "mixboot_nu8d_price.dta", clear

	
label var el_p11 "demand elast of self, 1% price change in self"
label var el_p12 "demand elast of public, 1% price change in self"
label var el_p13 "demand elast of private, 1% price change in self"
label var el_p14 "demand elast of doctor, 1% price change in self"

* the bootstrapped standard error of the elasticity E is the standard deviation of the empirical distribution of elasticity E calculated by bootstrapping the sample. 
sum el_p1*
sum el_p2*
sum el_p3* 
sum el_p4*



* elastciities by income quartiles 
sum el_pc11*
sum el_pc22*
sum el_pc33*
sum el_pc44*

histogram  del_p11, bin(40)
histogram  del_p12


*If female have a lower elasticity (more negative), the difference r(mu2female)-r(mu1male) should be negative. 1-pr(negative)
count if del_p11 <0
count if del_p22 <0
count if del_p33 <0
count if del_p44 <0


 ********************************************
 * UNINSTRUMENTED MODEL
 * CALCULATE ELASTICITIES OF PRICE OF THE UNINSTRUMENTED MODEL
 * The elasticities use the model  computed in Step4_estimate_RPM_adn_CL.do
 ********************************************
tab typeprov

* for each type of provider increase the price (for everybody) by 1%
forvalues i=1/4 {

preserve
set seed  563531

*list typeprov female p_i1 p_i2 p_i3 p_i4  p_i1_fem p_i2_fem p_i3_fem p_i4_fem in 1/3 //*to check changes are correct//*
*list typeprov female p_i1 p_i2 p_i3 p_i4  p_i1_fem p_i2_fem p_i3_fem p_i4_fem in 24/26 //*to check changes are correct//*

replace p_i1 = p_i1*(1.01) if typeprov==`i' 
replace p_i2 = p_i2*(1.01) if typeprov==`i'
replace p_i3 = p_i3*(1.01) if typeprov==`i' 
replace p_i4 = p_i4*(1.01) if typeprov==`i'

list typeprov p_i1 p_i2 p_i3 p_i4  in 1/3  //*to check changes are correct//*

mixlpred PR1`i', nrep(500)

gen el_p`i' = 100*(PR1`i'-PR0)/PR0

display "THESE ARE OWN AND CROSS PRICE ELASTICiTIES FOR 1% INCREASE IN PRICE IN TYPEPROVIDER= `i' "
*bysort quartile typeprov: sum el_p`i'
table typeprov, c(mean el_p`i' med el_p`i') format(%9.3f) 

*table typeprov female, c(mean el_p`i' med el_p`i') format(%9.3f) col row

*table typeprov quartile, c(mean el_p`i' med el_p`i') format(%9.3f) col row

restore 
}

********************************************************************************************
 * BOOTSTRAPPING PRICE ELASTICITY IN UN-INSTRUMENTED MODEL 
********************************************************************************************
** This do file bootstraps price elasticities of demand  and price elasticities of demand by income quantile and by gender
** and save the simulated elasticities in file mixboot_g.dta


cd "C:\Users\w1338843\Dropbox\AIID_Nigeria\Data_encrypted_decrypted\"
set seed 545415
local bsize=100 /* size of the iterations*/
global regressors "dist_dw_hf_2 dist_22   alt2-alt4"
global random "doctor_tot  generator2009 beds p_i1 p_i2 p_i3 p_i4  "
global control "idid choicesit quartile no_facility i1 i2 i3 i4 p_i1 p_i2 p_i3 p_i4 male female age age2 typeprov hh_id09 q01_10" 
set more off
tempname 3
postfile `3' iter   el_p11 el_p12 el_p13 el_p14  /// *self*
					el_p21 el_p22 el_p23 el_p24  /// *public*
					el_p31 el_p32 el_p33 el_p34  /// *private*
					el_p41 el_p42 el_p43 el_p44  /// *doctor*
					///
					///
                    using mixboot_noinstrumentation_price, replace   
					
forvalues iters1=1(1)`bsize' {
use tempdata_mixedlogit_vs4.dta, clear
*	replace waittime2009 = -waittime2009
*	replace nomed2009 = -nomed2009
keep idid no_facility d_ $control $regressors $random
bsample 1000, cluster(idid) idcluster(idnew)
replace choicesit =idnew


				noisily di "  ITER =" "`iters1'" "ESTIMATION OF RANDOM PARAMETER MODEL "
				* estimate RPM for bootstrapped sample 'iter'
			    mixlogit d_  $regressors  , group(choicesit) id(idnew) cluster(q01_10) rand($random) from(b47, copy)

	            mixlpred PR0, nrep(500)
	            
* CALCULATE  THE SIMULATED ELASTICITY BY CHANGING X BY 1%
* for each type of provider increase the price (for everybody) by 1%
forvalues i=1/4 {
noisily di "WE ARE GOING TO CHANGE PRICE FOR PROVIDER=" "`i'" 

local i=`i'     //*typeprovider whose price is being changed*//
preserve

*set seed 563531
replace p_i1 = p_i1*(1.01) if typeprov==`i' 
replace p_i2 = p_i2*(1.01) if typeprov==`i'
replace p_i3 = p_i3*(1.01) if typeprov==`i' 
replace p_i4 = p_i4*(1.01) if typeprov==`i'

*list typeprov p_i1 p_i2 p_i3 p_i4  in 1/3  //*to check changes are correct//*

mixlpred PR1`i', nrep(500)

gen el_p`i' = 100*(PR1`i'-PR0)/PR0


* own and cross elasticities: for each provider (whose price changes), store own elasticity and cross
* if price changes in i, what happen to # => E#z(i)

noisily di "===> OWN AND CROSS ELASTICITY: PRICE CHANGE IN PROVIDER=" "`i'" 

sum el_p`i' if typeprov==1
local el_p`i'1 = r(mean)
sum el_p`i' if typeprov==2
local el_p`i'2 = r(mean)
sum el_p`i' if typeprov==3
local el_p`i'3 = r(mean)
sum el_p`i' if typeprov==4
local el_p`i'4 = r(mean)

/*
noisily di "===> OWN AND CROSS ELASTICITY FOR MALE: PRICE CHANGE IN PROVIDER=" "`i'" 

sum el_p`i' if typeprov==1 & female==0
local el_p`i'1m = r(mean)
sum el_p`i' if typeprov==2 & female==0
local el_p`i'2m = r(mean)
sum el_p`i' if typeprov==3 & female==0
local el_p`i'3m = r(mean)
sum el_p`i' if typeprov==4 & female==0
local el_p`i'4m = r(mean)

noisily di "===> OWN AND CROSS ELASTICITY FOR FEMALE: PRICE CHANGE IN PROVIDER=" "`i'" 

sum el_p`i' if typeprov==1 & female==1
local el_p`i'1f = r(mean)
sum el_p`i' if typeprov==2 & female==1
local el_p`i'2f = r(mean)
sum el_p`i' if typeprov==3 & female==1
local el_p`i'3f = r(mean)
sum el_p`i' if typeprov==4 & female==1
local el_p`i'4f = r(mean)

* generate the difference between elasticity of female and male 
noisily di "===> GET THE DIFFERENCE IN PRICE ELASTICITIES FEMALE-MALE FOR A 1% PRICE CHANGE IN PROVIDER=" "`i'" 

ttest el_p`i' if typeprov==1, by(female)
local del_p`i'1 = r(mu_2)-r(mu_1)
ttest el_p`i' if typeprov==2, by(female)
local del_p`i'2 = r(mu_2)-r(mu_1)
ttest el_p`i' if typeprov==3, by(female)
local del_p`i'3 = r(mu_2)-r(mu_1)
ttest el_p`i' if typeprov==4, by(female)
local del_p`i'4 = r(mu_2)-r(mu_1)

* for each income quartile sum elasticity  within group x and store elasticity
* interpretation: price elasticity - changing price of provider i, what happen to provider # and quartile q => E#z(i)



forvalues q=1/4 {
sum el_p`i' if typeprov==1 & quartile==`q'
local el_pc`i'1`q' = r(mean)
sum el_p`i' if typeprov==2 & quartile==`q'
local el_pc`i'2`q' = r(mean)
sum el_p`i' if typeprov==3 & quartile==`q'
local el_pc`i'3`q' = r(mean)
sum el_p`i' if typeprov==4 & quartile==`q'
local el_pc`i'4`q' = r(mean)
}


foreach  q of numlist 1 2 3 4 {

noisily di "===> OWN AND CROSS ELASTICITY BY INCOME QUANTILE: PRICE CHANGE IN PROVIDER=" "`i'" "  INCOME QUARTILE=" "`q'"

sum el_p`i' if typeprov==1 & quartile==`q'
local el_pc`i'1`q' =r(mean)
sum el_p`i' if typeprov==2 & quartile==`q'
local el_pc`i'2`q' =r(mean)
sum el_p`i' if typeprov==3 & quartile==`q'
local el_pc`i'3`q' =r(mean)
sum el_p`i' if typeprov==4 & quartile==`q'
local el_pc`i'4`q' =r(mean)
}
*/

restore


}

local iter=`iters1'


post `3' (`iter')   (`el_p11') (`el_p12') (`el_p13') (`el_p14')  /// *self*
					(`el_p21') (`el_p22') (`el_p23') (`el_p24')  /// *public*
					(`el_p31') (`el_p32') (`el_p33') (`el_p34')  /// *private*
					(`el_p41') (`el_p42') (`el_p43') (`el_p44')  /// *doctor*
					
					
					
}
postclose `3'
		

		
use "mixboot_noinstrumentation_price.dta", clear

	
label var el_p11 "demand elast of self, 1% price change in self"
label var el_p12 "demand elast of public, 1% price change in self"
label var el_p13 "demand elast of private, 1% price change in self"
label var el_p14 "demand elast of doctor, 1% price change in self"

* the bootstrapped standard error of the elasticity E is the standard deviation of the empirical distribution of elasticity E calculated by bootstrapping the sample. 
sum el_p1*
sum el_p2*
sum el_p3* 
sum el_p4*

histogram  del_p11, bin(40)
histogram  del_p12


*If female have a lower elasticity (more negative), the difference r(mu2female)-r(mu1male) should be negative. 1-pr(negative)
count if del_p11 <0
count if del_p22 <0
count if del_p33 <0
count if del_p44 <0




